<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';
$user=$_SESSION['userbkknt'];
//ambil data user dari database
$query_cari_user=mysql_query("
    select namaUser, hpUser, emailUser, ttdUser
    from user
    where uname='$user'
");
$row_cari_user=mysql_fetch_array($query_cari_user);

#data parsingan
$idquotation=$_REQUEST['id'];

//ambil data customer dari database
$query_cari_cust=mysql_query("
    select c.idCustomer, c.namaCustomer, c.attnCustomer, c.alamatCustomer, c.telpCustomer, c.faxCustomer, i.text, q.noQuotation
    from quotation q, inquiry i, customer c
    where q.idQuotation='$idquotation' and i.no_inquiry=q.no_inquiry and c.idCustomer=i.idCustomer
");
$row_cari_cust=mysql_fetch_array($query_cari_cust);

#sertakan library FPDF dan bentuk objek
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->AliasNbPages();

#garis sebelah kanan

#setting judul laporan
$judul = "Quotation";

#tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,20, $judul, '0', 1, 'C');

#tampilkan data peminta dan user pembuat quotation
#first line
$pdf->SetFont('Arial','B','10');
$cell=array(20,75,20,75);
$pdf->Cell($cell[0],8, 'Date', 'LTB', 0, '');
$pdf->Cell($cell[1],8, ': '.date("M d, Y"), 'RTB', 0, '');
$pdf->Cell($cell[2],8, 'No', 'LTB', 0, '');
$pdf->Cell($cell[3],8, ': '.$row_cari_cust['noQuotation'], 'RTB', 0, '');
$pdf->Ln();
#second line
$pdf->Cell($cell[0],8, 'To', 'LTB', 0, '');
$pdf->Cell($cell[1],8, ': '.$row_cari_cust['namaCustomer'], 'RTB', 0, '');
$pdf->Cell($cell[2],8, 'Re', 'LTB', 0, '');
$pdf->Cell($cell[3],8, ': '.$row_cari_cust['text'], 'RTB', 0, '');
$pdf->Ln();
#third line
$pdf->Cell($cell[0],8, 'Attn', 'LTB', 0, '');
$pdf->Cell($cell[1],8, ': '.$row_cari_cust['attnCustomer'], 'RTB', 0, '');
$pdf->Cell($cell[2],8, 'Pages', 'LTB', 0, '');
$pdf->Cell($cell[3],8, ': '.$pdf->PageNo().' of {nb}', 'RTB', 0, '');
$pdf->Ln();
#fourth line
$pdf->Cell($cell[0],8, 'Copy', 'LTB', 0, '');
$pdf->Cell($cell[1],8, ': --- ', 'RTB', 0, '');
$pdf->Cell($cell[2],8, 'Telp', 'LTB', 0, '');
$pdf->Cell($cell[3],8, ': '.$row_cari_cust['telpCustomer'], 'RTB', 0, '');
$pdf->Ln();
#fifth line
$pdf->Cell($cell[0],8, 'From', 'LTB', 0, '');
$pdf->Cell($cell[1],8, ': '.$row_cari_user['namaUser'], 'RTB', 0, '');
$pdf->Cell($cell[2],8, 'Fax', 'LTB', 0, '');
$pdf->Cell($cell[3],8, ': '.$row_cari_cust['faxCustomer'], 'RTB', 0, '');
$pdf->Ln(15);

#tampilkan kata sambutan
$pdf->SetFont('Arial','','10');
$sambutan="We would like to thank you for your inquiry, and we are pleased to submit our quotation as follows:";
$pdf->Cell(0,5, 'Dear Sir,', '0', 1, '');
$pdf->Cell(0,5, $sambutan, '0', 1, '');
 
#setting header tabel
$header = array(
		array("label"=>"No", "length"=>8, "align"=>"C"),
		array("label"=>"Description", "length"=>96, "align"=>"C"),
		array("label"=>"Qty", "length"=>10, "align"=>"C"),
		array("label"=>"Unit Price", "length"=>38, "align"=>"C"),
		array("label"=>"Total Price", "length"=>38, "align"=>"C")
	);

#buat header tabel
$pdf->SetFont('Arial','B','10');
foreach ($header as $kolom) {
	$pdf->Cell($kolom['length'], 8, $kolom['label'], 1, '0', $kolom['align']);
}
$pdf->Ln();

#ambil data di tabel dan masukkan ke array
$query = "
select 
	barang.namaBarang as Description
	,quotation_item.qtyBarang as Qty
	,'IDR' as matauang1
	,format(quotation_item.priceItem,0) as 'unitprice'
	,'IDR' as matauang2
	,format(quotation_item.priceItem * quotation_item.qtyBarang,0) as totalprice
from 
	quotation
	,quotation_item
	,barang
where
	barang.idBarang=quotation_item.idBarang
	and quotation_item.idQuotation=quotation.idQuotation
	and quotation.idQuotation='$idquotation'
";
$sql = mysql_query ($query);
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
	array_push($data, $row);
}
 
#tampilkan data tabelnya
//set lebar kolom isi
$pdf->SetFont('Arial','','10');
$dataisi = array(
	array("length"=>$header[1][length],"align"=>"L"),
	array("length"=>$header[2][length],"align"=>"L"),
	array("length"=>($header[3][length]*(3/10)),"align"=>"L"),
	array("length"=>($header[3][length]*(7/10)),"align"=>"R"),
	array("length"=>($header[4][length]*(3/10)),"align"=>"L"),
	array("length"=>($header[4][length]*(7/10)),"align"=>"R")
	);
$j=0;
foreach ($data as $baris) {
	$i = 0;
	$pdf->Cell($header[0][length], 7, $j+1, 1, '0', 'L', $fill);
	foreach ($baris as $cell) {
		$pdf->Cell($dataisi[$i]['length'], 7, $cell, 1, '0', $dataisi[$i]['align']);
		$i++;
	}
	$pdf->Ln();
	$j++;
}

#menghitung subtotal, 10% VAT, dan grandtotal
$query = "
select 
	format(sum(quotation_item.priceItem * quotation_item.qtyBarang),0) as totalpricy
	,format((sum(quotation_item.priceItem * quotation_item.qtyBarang))*(10/100),0) as vat
	,format((sum(quotation_item.priceItem * quotation_item.qtyBarang))
	+((sum(quotation_item.priceItem * quotation_item.qtyBarang))*(10/100)),0) as total
from 
	quotation_item,quotation
where
        quotation.idQuotation='$idquotation'
	and quotation_item.idQuotation=quotation.idQuotation
";
$sql = mysql_query ($query);
if(!$sql) die(mysql_error());
$subtotal=mysql_fetch_array($sql);

#subtotal
$pdf->SetX(124);
$pdf->Cell($header[3][length], 7, 'Sub Total', 1, '0', 'R');
$pdf->Cell(($header[4][length]*(3/10)), 7, 'IDR', 1, '0', 'L');
$pdf->Cell(($header[4][length]*(7/10)), 7, $subtotal[0], 1, '1', 'R');

#10% VAT
$pdf->SetX(124);
$pdf->Cell($header[3][length], 7, '10% VAT', 1, '0', 'R');
$pdf->Cell(($header[4][length]*(3/10)), 7, 'IDR', 1, '0', 'L');
$pdf->Cell(($header[4][length]*(7/10)), 7, $subtotal[1], 1, '1', 'R');

#grandtotal
$pdf->SetFont('Arial','B','10');
$pdf->SetX(124);
$pdf->Cell($header[3][length], 7, 'TOTAL', 1, '0', 'R');
$pdf->Cell(($header[4][length]*(3/10)), 7, 'IDR', 1, '0', 'L');
$pdf->Cell(($header[4][length]*(7/10)), 7, $subtotal[2], 1, '1', 'R');

#note
$pdf->Cell(0, 5, 'Note:', 0, '1', '');
$query="
select keterangan 
from customer
where idCustomer='$row_cari_cust[idCustomer]'
";
$sql = mysql_query ($query);
$note=mysql_fetch_array($sql);
$note=explode("-",$note[0]);

$pdf->SetFont('Arial','','10');
foreach($note as $keterangan){
	$pdf->Cell(0, 5, ' - '.$keterangan, 0, '1', '');
}

#penutup
$text=array(
"Should you have any further questions, please do not hesitate to contact us.",
"Thank you for your kind attention and we look forward to hearing from you soon."
);
$pdf->Ln();
$pdf->Cell(0, 5, $text[0], 0, '1', '');
$pdf->Cell(0, 5, $text[1], 0, '1', '');
$pdf->Ln();
$pdf->Cell(0, 5, 'Yours Sincerely,', 0, '1', '');

$pdf->SetTextColor(49,86,12);
$pdf->SetFont('Arial','B','10');
$pdf->Cell(0, 5, 'PT. BUKAKA INTI aircon', 0, '1', '');
$pdf->Ln(10);
$user=$row_cari_user['namaUser'];
$mobile=$row_cari_user['hpUser'];
$email=$row_cari_user['emailUser'];
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','','10');
$pdf->Cell(0, 5, $user, 0, '1', '');
$pdf->Cell(0, 5, 'Mobile : '.$mobile, 0, '1', '');
$pdf->Cell(12, 5, 'Email : ', 0, '0', '');
$pdf->SetTextColor(0,0,255);
$pdf->SetFont('Arial','U','10');
$pdf->Cell(0, 5, $email, 0, '1', '');
$pdf->Cell(0, 5, 'http://www.bukakainti.com', 0, '1', '');

#footer
$pdf->SetY(-60);
$pdf->SetTextColor(49,86,12);
$pdf->SetFont('Arial','BU','10');
$pdf->Cell(0, 5, 'PT. BUKAKA INTI aircon', 0, '1', 'C');
$toko=array(
	array("Jl. Wisma Permai II / no. 29 Surabaya","Phone : 031-5992147, 5948857","Fax: 031-5913842","Email : sbyoffice@bukakainti.com")
	,array("Jl Kartini No 2C Jakarta","Phone : 021- 77217722","Fax: 021-77217673","Email : jktoffice@bukakainti.com")
	,array("Jl Jipang Raya no 35 Makassar", "Phone : 0411-8214940", "Fax: 0411-8214941", "Email : mksoffice@bukakainti.com")
	,array("Jl. Danau Tamblingan X/17 Bali","Phone : 0361-773335","Fax : 0361-773335","Email : balioffice@bukakainti.com")
);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','','9');
foreach($toko as $tokos){
	$pdf->Cell(57.5,5,$tokos[0],0,0,'');
	$pdf->Cell(47.5,5,$tokos[1],0,0,'');
	$pdf->Cell(35,5,$tokos[2],0,0,'');
	$pdf->Cell(50,5,$tokos[3],0,1,'');
}
$pdf->Cell(0, 5, 'http://www.bukakainti.com', 0, '1', 'C');


#output file PDF
$pdf->Output();
?>